package org.hbbTlias2025;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import java.util.*;

@Slf4j
public class testJWT {

    @Test
    public void testJWTGenerate() {

        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("id", 1);
        dataMap.put("username", "hbb");

        String jwt =Jwts.builder()
                .setSubject("hbb")
                .signWith(io.jsonwebtoken.SignatureAlgorithm.HS256, "aGJi")
                .addClaims(dataMap)
                .setExpiration(new Date(System.currentTimeMillis() + 3600 * 1000))
                .compact();
        log.info("JWT生成成功---" + jwt);

    }

    @Test
    public void testJWTParse() {
        String hbb = "eyJhbGciOiJIUzI1NiJ9" +
                ".eyJzdWIiOiJoYmIiLCJpZCI6MSwidXNlcm5hbWUiOiJoYmIiLCJleHAiOjE3NDIyMjYxMDR9" +
                ".RUGoqHg9yHDnkqoDWCMR4KnlPekgcgv45i-W9TWXtKc";

        Claims claims = Jwts.parser().setSigningKey("aGJi").parseClaimsJws(hbb).getBody();

        log.info("JWT解析成功---" + claims);

    }

//    增加一个对JWT令牌内容的解析测试
    @Test
    public void testJWTParse2() {
        String hbb = "eyJhbGciOiJIUzI1NiJ9" +
                ".eyJzdWIiOiJoYmIiLCJpZCI6MSwidXNlcm5hbWUiOiJoYmIiLCJleHAiOjE3NDIyMjYxMDR9" +
                ".RUGoqHg9yHDnkqoDWCMR4KnlPekgcgv45i-W9TWXtKc";
        Claims claims = Jwts.parser().setSigningKey("aGJi").parseClaimsJws(hbb).getBody();
        log.info("JWT解析成功---" + claims);

    }
}
